package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SpringLayout;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import messages.JobSignUpResult;
import model.Job;
import model.Role;
import model.User;
import model.WorkLoad;
import controller.Controller;
/**
 * Shows a single Job along with its list of volunteers.
 * 
 * @author James Gunter - jpgunter
 *
 */
public class JobView extends JPanel {
  public static int PREFFERED_HEIGHT = 400;
  public static int PREFFERED_WIDTH = 600;
  
  private JLabel my_duration_static_label;
  private JLabel my_duration_label;
  private JLabel my_park_static_label;
  private JLabel my_park_label;
  private JLabel my_light_desc_static_label;
  private JLabel my_light_desc_label;
  
  private JTable my_volunteers_table;
  
  private TableModel my_volunteers_table_model;
  
  private DateFormat my_date_format;
  private JLabel my_light_needed_filled_static_label;
  private JLabel my_light_needed_filled_label;
  private JLabel my_med_desc_static_label;
  private JLabel my_med_desc_label;
  private JLabel my_med_needed_filled_static_label;
  private JLabel my_med_needed_filled_label;
  private JLabel my_heavy_desc_static_label;
  private JLabel my_heavy_desc_label;
  private JLabel my_heavy_needed_filled_static_label;
  private JLabel my_heavy_needed_filled_label;
  
  private MainWindow my_parent;
  
  public JobView(final MainWindow the_parent, final Job the_job, final Controller the_controller, final User the_user) {
    my_parent = the_parent;
    
    setPreferredSize(new Dimension(600, 435));
    
    my_date_format = new SimpleDateFormat("M/d/y");
    
    setBackground(Color.WHITE);
    SpringLayout springLayout = new SpringLayout();
    setLayout(springLayout);
    
    JButton back = new JButton("Back");
    back.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        my_parent.back();
      }
    });
    springLayout.putConstraint(SpringLayout.NORTH, back, 5, SpringLayout.NORTH, this);
    springLayout.putConstraint(SpringLayout.WEST, back, 0, SpringLayout.WEST, this);
    add(back);
    
    JLabel my_job_title_label = new JLabel(the_job.getTitle());
    my_job_title_label.setHorizontalAlignment(JLabel.CENTER);
    my_job_title_label.setFont(new Font("Dialog", Font.BOLD, 22));
    my_job_title_label.setBackground(Color.LIGHT_GRAY);
    my_job_title_label.setOpaque(true);
    springLayout.putConstraint(SpringLayout.NORTH, my_job_title_label, 5, SpringLayout.NORTH, this);
    springLayout.putConstraint(SpringLayout.WEST, my_job_title_label, 0, SpringLayout.EAST, back);
    springLayout.putConstraint(SpringLayout.EAST, my_job_title_label, 0, SpringLayout.EAST, this);
    add(my_job_title_label);
    
    JLabel my_description_static_label = new JLabel("Description:");
    springLayout.putConstraint(SpringLayout.NORTH, my_description_static_label, 0, SpringLayout.SOUTH, my_job_title_label);
    springLayout.putConstraint(SpringLayout.WEST, my_description_static_label, 5, SpringLayout.WEST, this);
    add(my_description_static_label);
    
    JLabel my_description_label = new JLabel(the_job.getDescription());
    springLayout.putConstraint(SpringLayout.NORTH, my_description_label, 0, SpringLayout.NORTH, my_description_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_description_label, 6, SpringLayout.EAST, my_description_static_label);
    add(my_description_label);
    
    JLabel my_date_label = new JLabel(my_date_format.format(the_job.getDate()));
    add(my_date_label);
    
    my_duration_static_label = new JLabel("Duration:");
    springLayout.putConstraint(SpringLayout.WEST, my_duration_static_label, 0, SpringLayout.WEST, my_description_static_label);
    add(my_duration_static_label);
    
    JLabel my_date_desc_label = new JLabel("Date:");
    springLayout.putConstraint(SpringLayout.NORTH, my_duration_static_label, 2, SpringLayout.SOUTH, my_date_desc_label);
    springLayout.putConstraint(SpringLayout.NORTH, my_date_label, 0, SpringLayout.NORTH, my_date_desc_label);
    springLayout.putConstraint(SpringLayout.WEST, my_date_label, 6, SpringLayout.EAST, my_date_desc_label);
    springLayout.putConstraint(SpringLayout.NORTH, my_date_desc_label, 2, SpringLayout.SOUTH, my_description_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_date_desc_label, 0, SpringLayout.WEST, my_description_static_label);
    add(my_date_desc_label);
    
    my_duration_label = new JLabel("" +  the_job.getDuration());
    springLayout.putConstraint(SpringLayout.NORTH, my_duration_label, 0, SpringLayout.NORTH, my_duration_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_duration_label, 6, SpringLayout.EAST, my_duration_static_label);
    add(my_duration_label);
    
    my_park_static_label = new JLabel("Park:");
    springLayout.putConstraint(SpringLayout.NORTH, my_park_static_label, 2, SpringLayout.SOUTH, my_duration_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_park_static_label, 0, SpringLayout.WEST, my_description_static_label);
    add(my_park_static_label);
    
    my_park_label = new JLabel(the_job.getPark().getName());
    springLayout.putConstraint(SpringLayout.NORTH, my_park_label, 0, SpringLayout.NORTH, my_park_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_park_label, 6, SpringLayout.EAST, my_park_static_label);
    add(my_park_label);
    
    my_light_desc_static_label = new JLabel("Light Work Description:");
    springLayout.putConstraint(SpringLayout.NORTH, my_light_desc_static_label, 2, SpringLayout.SOUTH, my_park_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_light_desc_static_label, 0, SpringLayout.WEST, my_description_static_label);
    add(my_light_desc_static_label);
    
    my_light_desc_label = new JLabel(the_job.getLightWorkDescription());
    springLayout.putConstraint(SpringLayout.NORTH, my_light_desc_label, 0, SpringLayout.NORTH, my_light_desc_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_light_desc_label, 6, SpringLayout.EAST, my_light_desc_static_label);
    add(my_light_desc_label); 
    
    my_light_needed_filled_static_label = new JLabel("Filled/Needed:");
    springLayout.putConstraint(SpringLayout.NORTH, my_light_needed_filled_static_label, 2, SpringLayout.SOUTH, my_light_desc_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_light_needed_filled_static_label, 0, SpringLayout.WEST, my_description_static_label);
    add(my_light_needed_filled_static_label);
    
    my_light_needed_filled_label = new JLabel(the_job.getLightWorkFilled() + "/" + the_job.getLightWorkNeeded());
    springLayout.putConstraint(SpringLayout.NORTH, my_light_needed_filled_label, 0, SpringLayout.NORTH, my_light_needed_filled_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_light_needed_filled_label, 6, SpringLayout.EAST, my_light_needed_filled_static_label);
    add(my_light_needed_filled_label);
    
    my_med_desc_static_label = new JLabel("Medium Work Description:");
    springLayout.putConstraint(SpringLayout.NORTH, my_med_desc_static_label, 2, SpringLayout.SOUTH, my_light_needed_filled_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_med_desc_static_label, 0, SpringLayout.WEST, my_description_static_label);
    add(my_med_desc_static_label);
    
    my_med_desc_label = new JLabel(the_job.getMediumWorkDescription());
    springLayout.putConstraint(SpringLayout.NORTH, my_med_desc_label, 0, SpringLayout.NORTH, my_med_desc_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_med_desc_label, 6, SpringLayout.EAST, my_med_desc_static_label);
    add(my_med_desc_label); 
    
    my_med_needed_filled_static_label = new JLabel("Filled/Needed:");
    springLayout.putConstraint(SpringLayout.NORTH, my_med_needed_filled_static_label, 2, SpringLayout.SOUTH, my_med_desc_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_med_needed_filled_static_label, 0, SpringLayout.WEST, my_description_static_label);
    add(my_med_needed_filled_static_label);
    
    my_med_needed_filled_label = new JLabel(the_job.getMediumWorkFilled() + "/" + the_job.getMediumWorkNeeded());
    springLayout.putConstraint(SpringLayout.NORTH, my_med_needed_filled_label, 0, SpringLayout.NORTH, my_med_needed_filled_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_med_needed_filled_label, 6, SpringLayout.EAST, my_med_needed_filled_static_label);
    add(my_med_needed_filled_label);
    
    my_heavy_desc_static_label = new JLabel("Heavy Work Description:");
    springLayout.putConstraint(SpringLayout.NORTH, my_heavy_desc_static_label, 2, SpringLayout.SOUTH, my_med_needed_filled_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_heavy_desc_static_label, 0, SpringLayout.WEST, my_description_static_label);
    add(my_heavy_desc_static_label);
    
    my_heavy_desc_label = new JLabel(the_job.getHeavyWorkDescription());
    springLayout.putConstraint(SpringLayout.NORTH, my_heavy_desc_label, 0, SpringLayout.NORTH, my_heavy_desc_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_heavy_desc_label, 6, SpringLayout.EAST, my_heavy_desc_static_label);
    add(my_heavy_desc_label); 
    
    my_heavy_needed_filled_static_label = new JLabel("Filled/Needed:");
    springLayout.putConstraint(SpringLayout.NORTH, my_heavy_needed_filled_static_label, 2, SpringLayout.SOUTH, my_heavy_desc_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_heavy_needed_filled_static_label, 0, SpringLayout.WEST, my_description_static_label);
    add(my_heavy_needed_filled_static_label);
    
    my_heavy_needed_filled_label = new JLabel(the_job.getHeavyWorkFilled() + "/" + the_job.getHeavyWorkNeeded());
    springLayout.putConstraint(SpringLayout.NORTH, my_heavy_needed_filled_label, 0, SpringLayout.NORTH, my_heavy_needed_filled_static_label);
    springLayout.putConstraint(SpringLayout.WEST, my_heavy_needed_filled_label, 6, SpringLayout.EAST, my_heavy_needed_filled_static_label);
    add(my_heavy_needed_filled_label);
    
    
    //Only show the sign up button if the user is a volunteer.
    if(the_user.getUserRole() == Role.VOLUNTEER){
      //do not show it if they have already signed up for that job.
      if(!the_job.getVolunteers().containsKey(the_user)){
        final JComboBox my_work_load_selection = new JComboBox();
        springLayout.putConstraint(SpringLayout.NORTH, my_work_load_selection, 5, SpringLayout.SOUTH, my_heavy_needed_filled_label);
        springLayout.putConstraint(SpringLayout.WEST, my_work_load_selection, 0, SpringLayout.WEST, my_description_static_label);
        my_work_load_selection.setModel(new DefaultComboBoxModel(new String[] {"choose work load", "Light", "Medium", "Heavy"}));
        add(my_work_load_selection);
        
        JButton my_sign_up_btn = new JButton("Sign Up");
        my_sign_up_btn.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            WorkLoad workload = WorkLoad.LIGHT;
            if( ((String)my_work_load_selection.getSelectedItem()).equals("choose work load")){
              JOptionPane.showMessageDialog(the_parent.getMyMainFrame(), "Please select a work load.");
            } else {
              if( ((String)my_work_load_selection.getSelectedItem()).equals("Light")){
                workload = WorkLoad.LIGHT;
              } else if( ((String)my_work_load_selection.getSelectedItem()).equals("Medium")){
                workload = WorkLoad.MEDIUM;
              } else if( ((String)my_work_load_selection.getSelectedItem()).equals("Heavy")){
                workload = WorkLoad.HEAVY;
              }
              JobSignUpResult result =  the_controller.signUp(the_user, the_job, workload);
              if(result == JobSignUpResult.SUCCESS){
                JOptionPane.showMessageDialog(my_parent.getMyMainFrame(), "You are now signed up for the job");
                the_parent.home();
              } else {
                JOptionPane.showMessageDialog(my_parent.getMyMainFrame(), getFriendlyMessage(result));
              }
              
            }
          }

          private String getFriendlyMessage(JobSignUpResult result) {
            String message = "";
            switch(result){
            case BLACKBALLED:
              message = "You are blackballed and may not sign up";
              break;
            case JOB_FULL:
              message = "The current job is filled, please sign up for another job";
              break;
            case JOB_IN_PAST:
              message = "That job has already happened.";
              break;
            case NOT_A_VOLUNTEER:
              message = "You are not a volunteer";
              break;
            case OVERCOMMITED:
              message = "You are already signed up for a job on that day.";
              break;
            case WORKLOAD_FULL: 
              message = "The workload you are trying to sign up for is full";
              break;
            }
            return message;
          }
        });
        springLayout.putConstraint(SpringLayout.WEST, my_sign_up_btn, 6, SpringLayout.EAST, my_work_load_selection);
        springLayout.putConstraint(SpringLayout.SOUTH, my_sign_up_btn, 0, SpringLayout.SOUTH, my_work_load_selection);
        add(my_sign_up_btn);
      }
    } else {
      
      JScrollPane scrollPane = new JScrollPane();
      springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 16, SpringLayout.SOUTH, my_heavy_needed_filled_static_label);
      springLayout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, my_description_static_label);
      springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -39, SpringLayout.SOUTH, this);
      
      springLayout.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.EAST, this);
      add(scrollPane);
      
      my_volunteers_table = new JTable();
      
      String[] table_headers = new String[]{"Name", "E-mail", "WorkLoad"};
      
      String[][] table_data = new String[the_job.getVolunteers().size()][3];
      
      int i = 0;
      Map<User, WorkLoad> volunteers = the_job.getVolunteers();
      
      for(User vol:volunteers.keySet()){
        table_data[i][0] = vol.getUserName();
        table_data[i][1] = vol.getUserEmail();
        table_data[i][2] = WorkLoad.toString((WorkLoad)volunteers.get(vol));
        i++;    
      }
      
      my_volunteers_table_model = new DefaultTableModel(table_data, table_headers);
      
      my_volunteers_table.setModel(my_volunteers_table_model);
      my_volunteers_table.getColumnModel().getColumn(0).setPreferredWidth(150);
      my_volunteers_table.getColumnModel().getColumn(1).setPreferredWidth(250);
      my_volunteers_table.setColumnSelectionAllowed(true);
      scrollPane.setViewportView(my_volunteers_table);
    }
    
    
    
    
    
  }
}

